package com.sxhuayuan.parking.job;

import com.alibaba.fastjson.JSON;
import com.sxhuayuan.parking.dto.SumDTO;
import com.sxhuayuan.parking.service.OrderService;
import com.sxhuayuan.parking.service.RedisService;
import com.sxhuayuan.parking.utils.DateUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单定时统计任务
 * 
 * @author helong
 * @create 2019-04-10 14:12
 */
@Component
@Lazy(false)
public class OrderStatisticsJob {

	@Resource(name = "orderServiceImpl")
	OrderService orderService;

	@Resource(name = "redisServiceImpl")
	RedisService redisService;

	private static final String REDIS_COUNT_ORDER = "admin_count_order";

	/**
	 * 每天00:00:00 统计前一天订单与本月订单 然后存入Redis里
	 * 统计为全部车场订单
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void countOrders() {
		Date yesterdayStartTime = DateUtils.getStartTimeOfOneday(-1);
		Date startTimeOfCurrentMonth = DateUtils.getStartTimeOfCurrentMonth();
		Date yesterdayEndTime = DateUtils.getEndTimeOfOneday(-1);
		// 本月订单统计
		SumDTO sumDTO = orderService.countByDataTime(startTimeOfCurrentMonth, yesterdayEndTime, null);

		// 昨日订单统计
		SumDTO sumDTO1 = orderService.countByDataTime(yesterdayStartTime, yesterdayEndTime, null);

		List<SumDTO> list = new ArrayList<>();
		list.add(sumDTO);
		list.add(sumDTO1);

		String s = JSON.toJSONString(list);
		redisService.saveString(REDIS_COUNT_ORDER, s, DateUtils.nowToTomorrowSecond());
	}

}
